package com.wuhongyu.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.Brand;
import com.wuhongyu.entity.FindPhone;
import com.wuhongyu.entity.MobilephoneCutsom;
import com.wuhongyu.service.impl.BrandServiceImpl;
import com.wuhongyu.service.impl.MobilePhoneServiceImpl;
import com.wuhongyu.util.MyUtil;

@Controller
public class MobileController {

	@Autowired
	private MobilePhoneServiceImpl mobilePhoneServiceImpl;
	@Autowired
	private BrandServiceImpl brandServiceImpl;

	/**
	 * 条件查询以及查询商品列表
	 * @param model
	 * @param mobilephoneCutsom 条件查询的条件(name)
	 * @param currentPage 分页
	 * @return
	 */
	@RequestMapping("/phoneList")
	public String phoneList(Model model, @Param("MobilephoneCutsom") MobilephoneCutsom mobilephoneCutsom,
			@Param("currentPage") Integer currentPage) {
		PageHelper.startPage(MyUtil.getCurrentPage(currentPage), MyCommon.PAGESIZE);
		List<MobilephoneCutsom> phoneList = mobilePhoneServiceImpl.selectAll(mobilephoneCutsom);
		PageInfo<MobilephoneCutsom> page = new PageInfo<MobilephoneCutsom>(phoneList);
		model.addAttribute("phoneList", phoneList);
		model.addAttribute("page", page);
		model.addAttribute("findPhone", mobilephoneCutsom);
			return "admin/phone/phoneList";
	}
	
	@RequestMapping("/beforePhoneList")
	public String beforePhoneList(Model model, @Param("MobilephoneCutsom") MobilephoneCutsom mobilephoneCutsom,
			@Param("currentPage") Integer currentPage,HttpSession session) {
		PageHelper.startPage(MyUtil.getCurrentPage(currentPage), MyCommon.PAGESIZE);
		List<MobilephoneCutsom> phoneList = mobilePhoneServiceImpl.selectAll(mobilephoneCutsom);
		PageInfo<MobilephoneCutsom> page = new PageInfo<MobilephoneCutsom>(phoneList);
		model.addAttribute("phoneList", phoneList);
		model.addAttribute("page", page);
		model.addAttribute("findPhone", mobilephoneCutsom);
			return "phone/phoneList";
	}

	/**
	 * 修改(插入)商品
	 * @param model
	 * @param mobilephoneCutsom 前端提交过来的需要修改(插入)的数据
	 * @param currentPage  分页
	 * @param photo 商品图片
	 * @param findPhone 条件查询的条件
	 * @return
	 */
	@RequestMapping("/updateOrInsertPhone")
	public String updateOrInsertPhone(Model model, @Param("MobilephoneCutsom") MobilephoneCutsom mobilephoneCutsom,
			@Param("currentPage") Integer currentPage,
			@Param("photo")MultipartFile photo,
			@Param("FindPhone")FindPhone findPhone) {
		//这段代码不应该写在controller层0.0  但是太累了 不想动了
		//如果前端传来图片 
		if(photo.getSize()!=0) {
			//如果是修改的话,并且原来有图片就先删除原来的图片
			if(mobilephoneCutsom.getId()!=null && mobilephoneCutsom.getPic()!=null) {
				MyUtil.deleteSchedule(mobilephoneCutsom.getPic());
			}
			String fileName = MyUtil.getNewFileName(photo.getOriginalFilename());
			MyUtil.insertSchedule(photo, fileName);
			mobilephoneCutsom.setPic(fileName);
		}
		mobilePhoneServiceImpl.updateOrInsert(mobilephoneCutsom);
		//将条件放到商品的扩展类里面
		MobilephoneCutsom mm = new MobilephoneCutsom();
		mm.setBrandName(findPhone.getFindname());
		return this.phoneList(model, mm, currentPage);
	}

	/**
	 *修改或者插入之前的查询
	 * @param model
	 * @param currentPage 分页
	 * @param mobilephoneCutsom 查询条件
	 * @return
	 */
	@RequestMapping("/queryBeforeUpdateOrInsertPhone")
	public String queryBeforeUpdateOrInsertPhone(Model model, @Param("currentPage") Integer currentPage,
			@Param("MobilephoneCutsom") MobilephoneCutsom mobilephoneCutsom) {
		//查询所有的品牌,方便选择
		List<Brand> brandList = brandServiceImpl.selectAll();
		//如果是修改就把分页和条件都放进去
		if (mobilephoneCutsom.getId() != null && !"".equals(mobilephoneCutsom.getId())) {
			MobilephoneCutsom phone = mobilePhoneServiceImpl.queryById(mobilephoneCutsom.getId());
			model.addAttribute("phone", phone);
			model.addAttribute("currentPage", currentPage);
			model.addAttribute("findPhone", mobilephoneCutsom);
		}
		model.addAttribute("brandList", brandList);
		return "admin/phone/updateOrInsertPhone";
	}

	/**
	 * 删除单行记录
	 * @param model
	 * @param currentPage 分页
	 * @param mobilephoneCutsom 查询条件
	 * @return
	 */
	@RequestMapping("/deletePhone")
	public String deletePhone(Model model, @Param("currentPage") Integer currentPage,
			@Param("MobilephoneCutsom") MobilephoneCutsom mobilephoneCutsom) {
		mobilePhoneServiceImpl.deleteById(mobilephoneCutsom.getId());
		return this.phoneList(model, mobilephoneCutsom, currentPage);
	}
	
	
	/**
	 * 删除多行记录
	 * @param model
	 * @param currentPage 分页
	 * @param mobilephoneCutsom 条件
	 * @param checkId 多行id
	 * @return
	 */
	@RequestMapping("/deleteBathPhone")
	public String deleteBathPhone(Model model, @Param("currentPage") Integer currentPage,
			@Param("MobilephoneCutsom") MobilephoneCutsom mobilephoneCutsom,
			@Param("checkId")Integer[] checkId) {
		mobilePhoneServiceImpl.deleteBath(checkId);
		return this.phoneList(model, mobilephoneCutsom, currentPage);
	}
}
